package com.hadwinling.blog.controller;

import com.hadwinling.blog.entity.Admin;
import com.hadwinling.blog.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 用户中心
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/22 8:54
 */
@Controller
@SessionAttributes("currUser")
public class UserController {

    @Autowired
    private AdminService adminService;

    /**
     * 跳转到登录界面
     * @return
     */
    @GetMapping("/user/login")
    public String toLoginPage(){
        return "login";
    }

    /**
     * 执行登录操作
     * @param adminname
     * @param password
     * @param request
     * @param model
     * @return
     */
    @PostMapping("/user/dologin")
    public String login(String adminname,String password , HttpServletRequest request, Model model){
        Admin admin = adminService.selectOneByAdminnameAndPassword(adminname, password);
        if (admin!=null){
            model.addAttribute("currUser",admin);
            //跳转到首页
            return "redirect:/";
        }else{
            //密码错误
            return "redirect:/user/login?rslt=1";
        }
    }


    /**
     * 跳转到注册界面
     * @return
     */
    @GetMapping("/user/register")
    public String toRegisterPage(){
        return "register";
    }

    /**
     * 执行检查用户名是否唯一的操作
     * @param adminname
     * @return
     */
    @PostMapping("/checkname")
    @ResponseBody
    public String checkName(@RequestParam(name = "adminname") String adminname){
//        System.out.println("adminname:"+adminname);
        List<Admin> adminList = adminService.selectByAdminname(adminname);
        if (adminList.size()!=0){
            //用户名已经存在
            return "0";
        }else {
            //用户名不存在，可以进行注册或更改操作
            return "1";
        }
    }

    /**
     * 执行注册操作
     * @param admin
     * @return
     */
    @PostMapping("/user/register")
    public String register(Admin admin){
//        System.out.println("admin:"+admin);
        //为该账号添加判断是否是管理者还是用户：0：表示用户
        admin.setIsadmin(0);
        admin.setState(1);
        admin.setCreatetime(new Date());
        adminService.insertSelective(admin);
        return "redirect:/user/login";
    }

    /**
     * 注销
     * @param sessionStatus
     * @return
     */
    @GetMapping("/user/logout")
    public String logout(SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "login";
    }

    /**
     * 跳转到用户中心界面
     * @return
     */
    @GetMapping("/user/center")
    public String toUserCenterPage(){
        return "UserCenter/index";
    }


    /**
     * 验证是否登陆
     * @param request
     * @return
     */
    @GetMapping("/checkLogin")
    @ResponseBody
    public String checkLogin(HttpServletRequest request){
        System.out.println("检查用户是否登陆");
        if (request.getSession().getAttribute("currUser") == null){
            //没有登陆，告知用户去登陆
            System.out.println("no");
            return "0";
        }
        //已经登陆了，用户可以进行下一步操作
        return "1";
    }

}
